package com.cmw.auntapp.constant;

import java.util.Map;

public class BaseAPI {
    private static final int ENV_DEV = 1; // 1:dev开发环境
    private static final int ENV_TEST = 2; // 2:测试环境
    private static final int ENV_PROD = 3; // 3:生产环境

    private static final String DEV_BASE_URL = "http://192.168.0.138:9023"; //"http://192.168.10.28:9023";// ;// "http://192.168.31.57:9023"; //"http://192.168.0.138:9023";//开发环境API
    private static final String TEST_BASE_URL = "https://api.ip-vip.com"; //测试环境API
    private static final String PROD_BASE_URL = "https://api.lilijiazheng.com"; //生产环境API

    private static final int ENV = ENV_PROD; // 当前环境1:dev开发环境,2:测试环境,3:生产环境

    public static String getBaseUrl() {
        String url = DEV_BASE_URL;
        switch (ENV) {
            case ENV_DEV :{
                url = DEV_BASE_URL;
                break;
            } case ENV_TEST :{
                url = TEST_BASE_URL;
                break;
            } case ENV_PROD :{
                url = PROD_BASE_URL;
                break;
            }
        }
        return url;
    }

    public static String getAPI(String api){
       String url = getBaseUrl();
       return String.format("%s%s",url, api);
    }

    /**
     * 解析RestFull API,如： /v1/msgs/member-code/{phone}
     * @param api   api
     * @param params 参数
     * @param parNames pathvaribale 名
     * @return 返回解析后的URL
     */
    public static String parseAPI(String api, Map<String, String> params, String... parNames) {
        if (null == parNames || parNames.length == 0) {
            return api;
        }
        if (null == params || params.size() == 0) {
            return api;
        }
        for(String parName : parNames){
            String val = params.get(parName);
            api = api.replace(String.format("{%s}", parName), val);
        }
        return api;
    }
}
